local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local EquipExcel = require("excel.equip")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
local ItemDefine = require("bag.ItemDefine")
local Util = require("common.Util")
local EquipLogic = require("equip.EquipLogic")

EQUIP_HECHENG_LOG_MAX = 30
--装备合成帮助查询
function equipDetailQuery(human) 
	local descConfig = EquipExcel.detail[1]
	if not descConfig then return end
	
	local msgRet = Msg.gc.GC_EQUIP_HECHENG_DETAIL_QUERY
	msgRet.desc = descConfig.desc
	Msg.send(msgRet, human.fd)
end

local function sendQuery(human, pos)
	local msgRet = Msg.gc.GC_EQUIP_HECHENG_QUERY
	msgRet.pos = pos	
	local cnt = 0
    local count = 0
    msgRet.dot = {0,0,0,0}
	for key, value in ipairs(EquipExcel.hecheng)do 
		if pos == value.pos then 
			cnt = cnt + 1
            msgRet.list[cnt].id = key
			msgRet.list[cnt].itemData[0] = 2
			Grid.makeItem(msgRet.list[cnt].itemData[1], value.hechengID, 1, nil, nil, nil, nil, 5)
			Grid.makeItem(msgRet.list[cnt].itemData[2], value.needID, 1, nil, nil, nil, nil, 5)
			msgRet.list[cnt].cnt = value.cnt

            Grid.makeItem(msgRet.list[cnt].need[1], value.money[1][1], value.money[1][2])
            msgRet.list[cnt].need[0] = 1
		end

        local nowEquipCnt = BagLogic.getItemCnt(human, value.needID)
        if nowEquipCnt >= value.cnt then
            msgRet.dot[value.pos] = 1
        end
	end	

	msgRet.list[0] = cnt
    msgRet.dot[0] = 4
	Msg.send(msgRet, human.fd)
end

-- 装备合成信息查询
function equipHechengQuery(human, pos)
	if pos == 0 then
		for i = ItemDefine.EQUIP_SUBTYPE_WEAPON, ItemDefine.EQUIP_SUBTYPE_SHIPIN do
			sendQuery(human, i)
		end
	else
		sendQuery(human, pos)
	end
end

-- 合成装备
function equipHechengDo(human, id, indexList)

    print("  equipHechengDo    ", id )
    Util.printTable(indexList)
    local config = EquipExcel.hecheng[id]
    if not config then return end

    if config.cnt ~= #indexList then
        Broadcast.sendErr(human, Util.format(Lang.EQUIP_HECHENG_ERR_NOITEM,ItemDefine.getValue(needEquipID,"name")))
        return
    end
    -- 检查 装备下标是否正确

    for _, index in ipairs(indexList) do
        local grid = human.db.equipBag[index]
        if not grid or grid.id ~= config.needID then
            return Broadcast.sendErr(human, Util.format(Lang.EQUIP_HECHENG_ERR_NOITEM,ItemDefine.getValue(needEquipID,"name")))
        end
    end

    -- 判断消耗
    for  _, v in ipairs(config.money) do
        if BagLogic.getItemCnt(human, v[1]) < v[2] then
            return
        end
    end


    for _, v in ipairs(config.money) do
        BagLogic.delItem(human,  v[1], v[2], "equip_hecheng")
    end


    local equipGrid = Util.copyTable(human.db.equipBag[indexList[1]])
    -- 删除道具
    for k, index in ipairs(indexList) do
        EquipLogic.delEquip(human, index, "equip_hecheng")
    end

    equipGrid.id = config.hechengID
    EquipLogic.getNewAttrByItemID(config.hechengID, equipGrid.attr)

    EquipLogic.addByEquipGrid(human, equipGrid, "equip_hecheng")
    -- 通知客户端
    local msgRet = Msg.gc.GC_EQUIP_HECHENG_DO
    Grid.makeItem(msgRet.item, equipGrid.id, 1, nil, equipGrid, index, Grid.getOpflagAtBag(equipGrid.id))
    Msg.send(msgRet, human.fd)

    local equipTb = {}
    equipTb[config.hechengID] = 1
    equipHechengLog(human,equipTb, config.money)
end 


function equipHechengLog(human,equip,cost)
    local now = os.time()
    local logTb = {}
    logTb.equip = equip
    logTb.cost = cost
    logTb.time = now
    human.db.equipLogs = human.db.equipLogs or {}
    table.insert(human.db.equipLogs,1,logTb)
    human.db.equipLogs[EQUIP_HECHENG_LOG_MAX + 1] = nil
end

function equipLogsQuery(human)
    local msgRet = Msg.gc.GC_EUQIP_HECHENG_LOG_QUERY

    if human.db.equipLogs == nil or #human.db.equipLogs == 0 then
        msgRet.equipLogs[0] = 0
        Msg.send(msgRet,human.fd)
        return
    end

    local len = #human.db.equipLogs
    for i = 1,len do
        local v = human.db.equipLogs[i]
        local count = 0
        for equipID,equipCnt in pairs(v.equip) do
            Grid.makeItem(msgRet.equipLogs[i].equip,equipID,equipCnt, nil, nil, nil, nil, 5)
        end
        Grid.makeItem(msgRet.equipLogs[i].cost[1], v.cost[1],v.cost[2])
        msgRet.equipLogs[i].cost[0] = 1
        msgRet.equipLogs[i].time = v.time
        if i >= 30 then
            break
        end
    end
    msgRet.equipLogs[0] = len
    Msg.send(msgRet,human.fd)
end
